package com.mairuide._frame.utils.excellistener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.mairuide._frame.utils.ReflectUtils;
import com.mairuide._frame.utils.SpringUtil;
import com.mairuide.bill.DealFlow;
import com.mairuide.bill.DealFlowService;
import com.mairuide.bill.utils.DateUtils;
import com.mairuide.exceltitle_dict.enum2.FieldTypeEnum;
import com.mairuide.exceltitle_dict.enum2.PackageNameEnum;
import com.mairuide.exceltitle_dict.pojo.ExceltitleDict;
import com.mairuide.exceltitle_dict.service.ExceltitleDictService;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;

/**
 * Created by 敲代码的卡卡罗特
 * on 2019/1/8 11:08.
 * 资金管控平台的处理类  账户交易明细表
 */
public class BaseDealFlowExcelListener extends AnalysisEventListener {
    private  List<DealFlow> list = new ArrayList<DealFlow>();
    private  List<DealFlow> data = new ArrayList<DealFlow>();
    private Map<Integer, ExceltitleDict> exceltitleDictMap = new HashMap<>();
    private String titleStr="";
    private Map<String,Object> paraMap = new HashMap<String,Object>();  //参数MAP
    private Map<String,Object> resultMap = new HashMap<String,Object>();  //结果MAP
    @Override
    public void invoke(Object object, AnalysisContext context) {
        List<String> s = (List<String>)object;
        Integer rowNum = context.getCurrentRowNum();
        String titleString=StringUtils.join(s,",");
        if(rowNum==0){
            titleStr=titleString;
            exceltitleDictMap = getExceltitleDictByTitleHead(s);
            return;
        }
        if (exceltitleDictMap.size()==0) return;
        if(titleStr.equals(titleString)) return;
        if(StringUtils.isBlank(titleString.replaceAll(",",""))) return;
        DealFlow dealFlow = new DealFlow();
        for (int i=0;i<s.size();i++){
            Object content = s.get(i);
            if(i>=exceltitleDictMap.size()) break;
            ExceltitleDict exceltitleDict = exceltitleDictMap.get(i);
            if (exceltitleDict==null) continue;
            if (StringUtils.isBlank(s.get(i))) continue;
            String classField = exceltitleDict.getClassField();
            Class aClass = FieldTypeEnum.getFieldTypeEnumByVal(exceltitleDict.getFieldType());
            if (aClass.equals(Timestamp.class) ){
                Date d=DateUtils.getDate((String) content);
                if (d==null) continue;
                content = new Timestamp( d.getTime());
            }else if (aClass.equals(BigDecimal.class) ){
                content=new BigDecimal((String) content);
            }else if (aClass.equals(Integer.class) ){
                if("sign".equals(classField)){
                    if ("出取".contains(content.toString())){
                        content=1;
                    }else if ("进存".equals(content.toString())){
                        content=0;
                    }else {
                        content=0;
                    }
                }else{
                    content=Integer.valueOf(content.toString());
                }
            }
            ReflectUtils.setValue(dealFlow,DealFlow.class,classField,aClass,content);
        }
        if(StringUtils.isBlank(dealFlow.getAccountRivalCard())&&StringUtils.isNotBlank(dealFlow.getAccountRivalNumber())){
            dealFlow.setAccountRivalCard(dealFlow.getAccountRivalNumber());
        }
        if(StringUtils.isBlank(dealFlow.getAccountBodyCard())&&StringUtils.isNotBlank(dealFlow.getAccountBodyNumber())){
            dealFlow.setAccountBodyCard(dealFlow.getAccountBodyNumber());
        }
        data.add(dealFlow);
        if(data.size()>=10000){
            //list.addAll(data);
            doSomething();
            Integer size=0;
            if (resultMap.containsKey("successNum")){
                size = (Integer) resultMap.get("successNum");
                size+=data.size();
            }else{
                size=data.size();
            }
            resultMap.put("successNum",size);
            data = new ArrayList<DealFlow>();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        //list.addAll(data);
        Integer size=0;
        if (resultMap.containsKey("successNum")){
            size = (Integer) resultMap.get("successNum");
            size+=data.size();
        }else{
            size=data.size();
        }
        resultMap.put("successNum",size);
        doSomething();
        data = new ArrayList<DealFlow>();
    }
    public void doSomething(){
        for (int i = 0; i < data.size(); i++) {
            DealFlow dealFlow = data.get(i);
            dealFlow.setUuid((String)paraMap.get("uuid"));
            dealFlow.setCasemanageId((String)paraMap.get("uid"));
            dealFlow.setCreateId((Integer) paraMap.get("platManagerId"));
            dealFlow.setCreateTime(new Timestamp(System.currentTimeMillis()));
            dealFlow.setIsDelete(0);
        }
        DealFlowService dealFlowService = SpringUtil.getBean(DealFlowService.class);
        dealFlowService.batchSave(data);
    }


    /**
     * 根据标题头返回对应的数据库的行对象
     * @param titles
     * @return
     */
    public Map<Integer, ExceltitleDict> getExceltitleDictByTitleHead(List<String> titles){
        ExceltitleDictService exceltitleDictService = SpringUtil.getBean(ExceltitleDictService.class);
        Map<Integer, ExceltitleDict> map = exceltitleDictService.getExceltitleDictByTitleHead(titles, PackageNameEnum.DEALFLOW.getPackageName());
        return map;
    }

    public List<DealFlow> getList() {
        return list;
    }

    public void setList(List<DealFlow> list) {
        this.list = list;
    }

    public Map<String, Object> getParaMap() {
        return paraMap;
    }

    public void setParaMap(Map<String, Object> paraMap) {
        this.paraMap = paraMap;
    }

    public Map<String, Object> getResultMap() {
        return resultMap;
    }

    public void setResultMap(Map<String, Object> resultMap) {
        this.resultMap = resultMap;
    }
}
